<?php
// +----------------------------------------------------------------------
// | motto: 不懈努力，只为遇见更好的自己
// +----------------------------------------------------------------------
// | Copyright: (c) 2020 http://www.slong.ink All rights reserved.
// +----------------------------------------------------------------------
// | Licensed: http://www.apache.org/licenses/LICENSE-2.0
// +----------------------------------------------------------------------
// | Author: slongzhang <slongzhang@qq.com>
// +----------------------------------------------------------------------
// | Date: 2023/11/4
// +----------------------------------------------------------------------
// | Time: 15:02
// +----------------------------------------------------------------------

namespace Extend\facade;

use Illuminate\Config\Repository;

class Config
{
    private static $facadeClient = null;
    // 多级目录分割符
    private static $multiLevelSeparator = '__';

    // 初始化实例
    private static function facadeInitInstance() {
        if (static::$facadeClient === null) {
            static::$facadeClient = new Repository();
        }
    }

    // 获取实例
    private static function facadeGetClient()
    {
        if (static::$facadeClient === null) {
            // 默认配置
            $configPath = getConfigPath();
            if (is_dir($configPath)) {
                // 加载配置
                static::facadeInit($configPath);
            }
        }

        return static::$facadeClient;
    }

    // 内部加载配置文件
    private static function facadeAddConfigFile($configFile, $configPrefix = []) {
        if (pathinfo($configFile, PATHINFO_EXTENSION) === 'php') {
            $configName = pathinfo($configFile, PATHINFO_FILENAME);
            if (is_array($configPrefix) && !empty($configPrefix)) {
                $configName = implode(static::$multiLevelSeparator, $configPrefix) . static::$multiLevelSeparator . $configName;
            }
            if (!static::has($configName)) {
                try {
                    $configValues = include $configFile;
                    static::set($configName, $configValues);
                }
                catch(\Exception $e) {
                    // 获取异常发生的文件和行号
                    echo PHP_EOL.'异常发生在文件 ' . $e->getFile() . ' 的第 ' . $e->getLine() . ' 行。'.PHP_EOL;
                    print_r($e->getMessage());
                    echo PHP_EOL;
                }
            }
        }
    }

    // 手动初始化
    public static function facadeInit($configPath = null, $configPrefix = []) {
        // 如果加载配置文件时还没实例则先进行实例
        if (static::$facadeClient === null) {
            static::facadeInitInstance();
        }
        // 如果是空目录则返回空
        if (empty($configPath)) {
            return null;
        }
        // 判断$configPath 是文件还是文件夹
        if (is_dir($configPath)) {
            // 遍历当前文件夹
            $configFiles = scandir($configPath);
            foreach ($configFiles as $configFile) {
                if ($configFile == '.' || $configFile == '..') {
                    continue;
                }
                $configFilePath = $configPath . DIRECTORY_SEPARATOR . $configFile;
                if (is_dir($configFilePath)) {
                    $prefix = $configPrefix;
                    $prefix[] = $configFile;
                    if (count($prefix) > 3) {
                        // 多层配置只加载到第三层
                        continue;
                    }
                    static::facadeInit($configFilePath, $prefix);
                }
                else {
                    static::facadeAddConfigFile($configFilePath, $configPrefix);
                }
            }
        }
        else {
            static::facadeAddConfigFile($configPath, $configPrefix);
        }
    }

    // 魔术方法
    public static function __callStatic($method, $arguments)
    {
        $client = static::facadeGetClient();

        return call_user_func_array([$client, $method], $arguments);
    }
}
